#include <mysql/mysql.h>
#include <string>
#include <jsoncpp/json/json.h>
#include "../comm/httplib.h"
#include "cookie.hpp"
#include "head_mysql.hpp"

using namespace httplib;

void insert_into_db(const Request &req, Response &res)
{
    // 检查 Cookie
    if (!check_cookie(req))
    {
        res.status = 401; // 未授权
        res.set_content("未授权，请登录", "text/plain;charset=utf-8");
        return;
    }

    Json::Value data;
    Json::Reader reader;
    if (!reader.parse(req.body, data))
    {
        res.status = 400; // Bad Request
        res.set_content("解析JSON失败: " + reader.getFormattedErrorMessages(), "text/plain");
        return;
    }
    
    MYSQL *conn = connect_db();

    // Construct the SQL query with placeholders
    const std::string query = "INSERT INTO oj_questions (title, star, `desc`, header, tail) VALUES (?, ?, ?, ?, ?)";

    MYSQL_STMT *stmt = mysql_stmt_init(conn);
    if (stmt == nullptr)
    {
        res.status = 500; // Internal Server Error
        res.set_content("Statement初始化失败", "text/plain");
        mysql_close(conn);
        return;
    }

    if (mysql_stmt_prepare(stmt, query.c_str(), query.length()))
    {
        res.status = 500; // Internal Server Error
        res.set_content("Statement准备失败: " + std::string(mysql_stmt_error(stmt)), "text/plain");
        mysql_stmt_close(stmt);
        mysql_close(conn);
        return;
    }

    // Bind parameters
    MYSQL_BIND bind[5];
    memset(bind, 0, sizeof(bind));

    std::string title = data["title"].asString();
    std::string star = data["star"].asString();
    std::string desc = data["desc"].asString();
    std::string header = data["header"].asString();
    std::string tail = data["tail"].asString();

    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = (char *)title.c_str();
    bind[0].buffer_length = title.length();

    bind[1].buffer_type = MYSQL_TYPE_STRING;
    bind[1].buffer = (char *)star.c_str();
    bind[1].buffer_length = star.length();

    bind[2].buffer_type = MYSQL_TYPE_STRING;
    bind[2].buffer = (char *)desc.c_str();
    bind[2].buffer_length = desc.length();

    bind[3].buffer_type = MYSQL_TYPE_STRING;
    bind[3].buffer = (char *)header.c_str();
    bind[3].buffer_length = header.length();

    bind[4].buffer_type = MYSQL_TYPE_STRING;
    bind[4].buffer = (char *)tail.c_str();
    bind[4].buffer_length = tail.length();

    if (mysql_stmt_bind_param(stmt, bind))
    {
        res.status = 500; // Internal Server Error
        res.set_content("参数绑定失败: " + std::string(mysql_stmt_error(stmt)), "text/plain");
        mysql_stmt_close(stmt);
        mysql_close(conn);
        return;
    }

    // Execute the query
    if (mysql_stmt_execute(stmt))
    {
        res.status = 500; // Internal Server Error
        res.set_content("SQL语句执行失败: " + std::string(mysql_stmt_error(stmt)), "text/plain");
    }
    else
    {
        res.status = 200; // OK
        res.set_content("数据插入成功.", "text/plain");
    }

    mysql_stmt_close(stmt);
    mysql_close(conn);
}